﻿using Dapper;
using PersonalWebsite.Core.CQRS;
using PersonalWebsite.Core.Result;

namespace PersonalWebsite.Application.Queries.Website;

public record GetWebsiteInfoVO
{
    public string Time { get; init; }
    public int RunDay { get; init; }
    public int PostCount { get; init; }
    public string Desc { get; init; }
}

public class GetWebsiteInfoQuery : IQuery<ApiResult<GetWebsiteInfoVO>, GetWebsiteInfoVO>
{ }

public class GetWebsiteInfoQueryHandler : QueryHandler<GetWebsiteInfoQuery, ApiResult<GetWebsiteInfoVO>, GetWebsiteInfoVO>
{
    private readonly IDBConnectionFactory dBConnectionFactory;

    public GetWebsiteInfoQueryHandler(IDBConnectionFactory dBConnectionFactory) : base(dBConnectionFactory)
    {
        this.dBConnectionFactory = dBConnectionFactory;
    }

    public override async Task<ApiResult<GetWebsiteInfoVO>> Handle(GetWebsiteInfoQuery request, CancellationToken cancellationToken)
    {
        using var connection = dBConnectionFactory.GetDbConnection();
        int postCount = await connection.ExecuteScalarAsync<int>("SELECT  COUNT(1) FROM WP_Post");

        var result = new GetWebsiteInfoVO()
        {
            Time = "25/2",
            RunDay = (DateTime.Now.Date - Convert.ToDateTime("2025-02-20")).Days,
            PostCount = postCount,
            Desc = "这是一个基于NetCore8,Vue3和Typescript构建的网站。使用Docker、Nginx部署在Linux服务器上。"
        };

        return ApiResult<GetWebsiteInfoVO>.Success(result);
    }
}